// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { name } = require("./package.json");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const publicPath = process.env.NODE_ENV === "production" ? "https://qiankun.umijs.org/" : "http://localhost:7777/";
console.log("/环境变量", process.env.NODE_ENV);
console.log("代理地址", process.env.VUE_APP_PROXY_TARGET);
module.exports = {
  devServer: {
    // 监听端口
    port: 7777,
    compress: true, // 是否启动压缩 gzip
    // 关闭主机检查，使微应用可以被 fetch
    disableHostCheck: true,
    // 配置跨域请求头，解决开发环境的跨域问题
    headers: {
      "Access-Control-Allow-Origin": "*"
    }
  },
  productionSourceMap: false,
  configureWebpack: config => {
    config.resolve.alias = {
      "@": path.resolve(__dirname, "src")
    };
    // 生产环境去除console.log
    if (process.NODE_ENV === "production") {
      config.plugins.push(
        new UglifyJsPlugin({
          uglifyOptions: {
            //生产环境自动删除console
            compress: {
              // eslint-disable-next-line camelcase
              drop_debugger: true,
              // eslint-disable-next-line camelcase
              drop_console: true,
              // eslint-disable-next-line camelcase
              pure_funcs: ["console.log"]
            }
          },
          sourceMap: false,
          parallel: true
        })
      );
    }
    // 代码分割
    config.optimization.splitChunks.cacheGroups = {
      vendor: {
        chunks: "all",
        test: /node_modules/,
        name: "vendor",
        minChunks: 1,
        maxInitialRequests: 5,
        minSize: 0,
        priority: 100 // 优先级
      },
      common: {
        chunks: "all",
        test: /[\\/]src[\\/]js[\\/]/,
        name: "common",
        minChunks: 2,
        maxInitialRequests: 5,
        minSize: 0,
        priority: 60
      },
      styles: {
        name: "styles",
        test: /\.(sa|sc|c)ss$/,
        chunks: "all",
        enforce: true
      },
      runtimeChunk: {
        name: "manifest"
      }
    };
    Object.assign(config.output, {
      // 微应用的包名，这里与主应用中注册的微应用名称一致
      library: name,
      // 将你的 library 暴露为所有的模块定义下都可运行的方式
      libraryTarget: "umd",
      // 按需加载相关，设置为 webpackJsonp_微应用名称 即可
      jsonpFunction: `webpackJsonp_${name}`
    });
  },
  chainWebpack: config => {
    config.module
      .rule("fonts")
      .use("url-loader")
      .loader("url-loader")
      .options({
        limit: 4096, // 小于4kb将会被打包成 base64
        fallback: {
          loader: "file-loader",
          options: {
            name: "fonts/[name].[hash:8].[ext]",
            publicPath
          }
        }
      })
      .end();
    config.module
      .rule("images")
      .use("url-loader")
      .loader("url-loader")
      .options({
        limit: 4096, // 小于4kb将会被打包成 base64
        fallback: {
          loader: "file-loader",
          options: {
            name: "img/[name].[hash:8].[ext]",
            publicPath
          }
        }
      });
  }
};
